/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler
 * File: UpdateHandler.java
 * Created: Oct 19, 2005 12:44:01 PM
 * Author: johnny 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler;

import java.beans.PropertyDescriptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.homedns.mpetmaya.tools.LangUtil;

import com.amkai.aes.aescommon.dictionary.SharedDictionaryManagerCommon;
import com.amkai.aes.aescommon.dictionary.SharedKind;
import com.amkai.aes.aescommon.service.binary.job.UpdateJob;
import com.amkai.aes.aescommon.service.result.IdResult;
import com.amkai.aes.generatedbeans.dictionary.Item;
import com.amkai.aes.server.dictionary.SharedDictionaryManager;
import com.amkai.aes.server.domaincommiter.dictionaryeditor.StateCodesCommiter;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.BeanManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.rad.designerbase.beans.RADBean;

/**
 * UpdateHandler
 * @author johnny
 * Description:
 *
 */
public class UpdateHandler implements JobObjectHandler<UpdateJob, RADBean, IdResult> {

	private static final Log log = LogFactory.getLog(UpdateHandler.class);

	
	/**
	 * 
	 * Method: process 
	 * @param job
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public IdResult process(UpdateJob job, RADBean object) throws Exception {
		IdResult result = new IdResult();
		SessionManager sm = SiteManager.Start();
		try {
//			sm.openSession(LogInterceptor.newInstance(null, null, sm.getFactory().getSessionFactory()));
			sm.openSession();
			sm.openTransaction();
			// KKORNEL: AES-4131 - Updating Dictiornary Items
			Item item = LangUtil.as(Item.class,object);
			// KKORNEL: AES-11533
			if (item == null) {
				sm.updateV(object);
			}
			else {
				if (item.getChangedProperties() == null) {
					log.error("The bean is not changed!!!");
					return result;
				}				
				PropertyDescriptor desc = BeanManager.getType(item.getClass(), "id");
				Item item0 = (Item)sm.get(item.getClass(), (Long)desc.getReadMethod().invoke(item));
				item0.enableChangeLog();
				int size = item.getChangedProperties().size();
				BeanManager.copy(item, item0, item.getChangedPropertyNames().toArray(new String[size]));
				SharedDictionaryManager sdm = new SharedDictionaryManager(sm);
				// KKORNEL: AES-5778
				if (SharedDictionaryManagerCommon.getInstance().canHaveStateCodes(item.getKind())) {
					if (SharedDictionaryManagerCommon.getInstance().getSharedKind(item.getKind()) == SharedKind.PARTIALLY_SHARED) {
						item0.setItemGroup(item.getItemGroup());
						if (item0.getItemGroup() != null) {
							item0.getItemGroup().setItemStateCodes(item.getItemGroup().getItemStateCodes());
						}
					} else {
						item0.setItemStateCodes(item.getItemStateCodes());
					}
				}
				item0.setChangedKind(item.getChangedKind());
				sdm.saveItem(item0);
				// KKORNEL: AES-5778
				StateCodesCommiter.commitItemStateCodes(sm, item0);
			}
			sm.commitTransaction();

		}
		catch (Exception e) {
			sm.rollbackTransaction();
			log.error("",e);
			throw e;
		}
		finally {
			sm.closeSession();
		}
		

		return result;
	}


}
